home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / APLocation / Sources / MacOS_UMemory.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  5.5 KB  |  275 lines

  1. /*==================================================================
  2.     File:        MacOS_UMemory.h
  3.  
  4.     Contains:    Implementation of various Memory Manager utilities
  5.  
  6.     Written by:    Ed Reed, Linden Siahaan
  7.  
  8.     Copyright:    2000 Connectix Corporation
  9. ==================================================================*/
  10.  
  11. #include "MacOS_Exceptions.h"
  12. #include "MacOS_Namespaces.h"
  13.  
  14. #ifndef __MACMEMORY__
  15. #include <MacMemory.h>
  16. #endif
  17.  
  18. #include <new>
  19.  
  20. #pragma once
  21.  
  22. CTX_Begin_Namespace_MacOS
  23.  
  24. /*------------------------------------------------------------------
  25.     StHandleLocker
  26.  
  27.     This class implements a stack-based object which locks a handle.
  28.     The destructor automatically unlocks the handle when the object
  29.     goes out of scope.
  30.  
  31.     To use:
  32.         Simply create a variable of type StHandleLocker within the
  33.         scope you want access the locked handle. The destructor
  34.         will automatically be called at the end of the scope.
  35. ------------------------------------------------------------------*/
  36.  
  37. class StHandleLocker
  38. {
  39.     public:
  40.         StHandleLocker(Handle inMacHandle) : mMacHandle(inMacHandle)
  41.         {
  42.             mState = ::HGetState(inMacHandle);
  43.             ::HLock(inMacHandle);
  44.             check (MemError() == noErr);
  45.         }
  46.  
  47.         ~StHandleLocker()
  48.         {
  49.             ::HSetState(mMacHandle, mState);
  50.         }
  51.  
  52.     private:
  53.         Handle    mMacHandle;
  54.         SInt8    mState;
  55. };
  56.  
  57.  
  58. /*------------------------------------------------------------------
  59.     StHandleDisposer
  60.  
  61.     This class implements a stack-based object which automatically
  62.     disposes of a handle when the object goes out of scope.
  63.  
  64.     To use:
  65.         Simply create a variable of type StHandleDisposer within the
  66.         scope you want access the handle. The destructor will
  67.         automatically be called at the end of the scope.
  68. ------------------------------------------------------------------*/
  69.  
  70. class StHandleDisposer
  71. {
  72.     public:
  73.  
  74.         StHandleDisposer()
  75.         {
  76.             Set(NULL);
  77.         }
  78.  
  79.         StHandleDisposer(
  80.             Handle         inData)
  81.         {
  82.             Set(inData);
  83.         }
  84.  
  85.         ~StHandleDisposer()
  86.         {
  87.             if (mData != NULL)
  88.                 ::DisposeHandle(mData);
  89.         }
  90.  
  91.         void
  92.         Set(
  93.             Handle         inData)
  94.         {
  95.             mData = inData;
  96.         }
  97.  
  98.         operator
  99.         Handle() const
  100.         {
  101.             return mData;
  102.         }
  103.  
  104.     protected:
  105.         Handle                    mData;
  106. };
  107.  
  108.  
  109. /*------------------------------------------------------------------
  110.     StPtrDisposer
  111.  
  112.     This class implements a stack-based object which automatically
  113.     disposes of a Macintosh Ptr when the object goes out of scope.
  114.  
  115.     To use:
  116.         Simply create a variable of type StPtrDisposer within the
  117.         scope you want access the Ptr. The destructor will automatically
  118.         be called at the end of the scope.
  119. ------------------------------------------------------------------*/
  120.  
  121. class StPtrDisposer
  122. {
  123.     public:
  124.         StPtrDisposer()
  125.         {
  126.             Set(NULL);
  127.         }
  128.  
  129.         StPtrDisposer(
  130.             Ptr             inData)
  131.         {
  132.             Set(inData);
  133.         }
  134.  
  135.         ~StPtrDisposer()
  136.         {
  137.             if (mData != NULL)
  138.                 ::DisposePtr(mData);
  139.         }
  140.  
  141.         void
  142.         Set(
  143.             Ptr             inData)
  144.         {
  145.             mData = inData;
  146.         }
  147.  
  148.         operator
  149.         Ptr() const
  150.         {
  151.             return mData;
  152.         }
  153.  
  154.     protected:
  155.         Ptr                        mData;
  156. };
  157.  
  158.  
  159. /*------------------------------------------------------------------
  160.     StArray<Type_>
  161.  
  162.     This template class implements a stack-based object which wraps
  163.     a Ptr to the given type or an array of the given type. The
  164.     destructor automatically disposes of the pointer when the
  165.     object goes out of scope.
  166. ------------------------------------------------------------------*/
  167.  
  168. template <class Type_> class StArray
  169. {
  170.     public:
  171.         StArray(
  172.             ItemCount    inArraySize = 1,
  173.             Boolean        inThrowFail = true,
  174.             Boolean        inClearBytes = false )
  175.         {
  176.             if (inClearBytes)
  177.             {
  178.                 mPtr = reinterpret_cast<Type_*>( ::NewPtrClear( sizeof(Type_) * inArraySize ) );
  179.             }
  180.             else
  181.             {
  182.                 mPtr = reinterpret_cast<Type_*>( ::NewPtr( sizeof(Type_) * inArraySize ) );
  183.             }
  184.  
  185.             if (inThrowFail)
  186.             {
  187.                 if (mPtr == NULL)
  188.                     throw std::bad_alloc();
  189.             }
  190.         }
  191.  
  192.         ~StArray()
  193.         {
  194.             if (mPtr != NULL)
  195.             {
  196.                 ::DisposePtr( reinterpret_cast<Ptr>(mPtr) );
  197.             }
  198.         }
  199.  
  200.             // type coersions
  201.         operator Type_*()
  202.         {
  203.             return mPtr;
  204.         }
  205.         
  206.         Type_&
  207.         operator[](
  208.             ItemCount i)
  209.         {
  210.             return mPtr[i];
  211.         }
  212.         
  213.         operator void*()
  214.         {
  215.             return reinterpret_cast<void*>(mPtr);
  216.         }
  217.  
  218.         Type_
  219.         Get() const
  220.         {
  221.             return mPtr;
  222.         }
  223.  
  224.     protected:
  225.         Type_*        mPtr;
  226. };
  227.  
  228.  
  229. CTX_End_Namespace_MacOS
  230.  
  231.  
  232. /*==================================================================
  233.     Change History (most recent first):
  234.  
  235.     $Log: MacOS_UMemory.h,v $
  236.     Revision 1.13  2000/07/07 05:12:08  traut
  237.     Carbon-related code cleanup.
  238.     
  239.     Revision 1.12  2000/05/30 23:09:52  cstory
  240.     Moved a lot of stuff to the new MacOS_Accessors.h header
  241.     
  242.     Revision 1.11  2000/05/25 18:05:08  cstory
  243.     Moved in class CZonerSwitcher from VirtualPCMiscUtils.h and re-wrote it
  244.  
  245.     Revision 1.10  2000/05/23 16:47:48  cstory
  246.     Added dummy inlines for memory locking and unlocking under Carbon
  247.  
  248.     Revision 1.9  2000/05/17 16:29:39  cstory
  249.     Added #pragma once
  250.  
  251.     Revision 1.8  2000/05/16 18:53:02  cstory
  252.     Added setters and casting operators to StHandleDisposer and StPtrDisposer
  253.  
  254.     Revision 1.7  2000/05/15 22:56:54  cstory
  255.     Added dummy inlines for HoldMemory() and UnholdMemory()
  256.  
  257.     Revision 1.6  2000/05/12 15:48:11  cstory
  258.     Added StHandleDisposer and StPtrDisposer classes
  259.  
  260.     Revision 1.5  2000/03/16 19:19:53  reed
  261.     if StArray fails, throw std::bad_alloc instead of PowerPlant exception
  262.  
  263.     Revision 1.4  2000/03/09 19:07:37  lsiahaan
  264.     Correctly implemented MacOS namespaces, by including the header file that defines the macros.
  265.  
  266.     Revision 1.3  2000/03/09 08:53:09  traut
  267.     Undid previous checkin. It broke VPC, VGS and BossHogg.
  268.  
  269.     Revision 1.2  2000/03/09 02:10:21  lsiahaan
  270.     Implemented MacOS namespace, added StArray class
  271.  
  272.     Revision 1.1  1998/12/03 21:41:22  reed
  273.     First Checked In.
  274. ==================================================================*/
  275.